<%@ page import="org.apache.commons.fileupload.servlet.ServletFileUpload" %>
<%@ page import="org.apache.commons.fileupload.disk.DiskFileItemFactory" %>
<%@ page import="org.apache.commons.fileupload.FileItem" %>
<%@ page import="java.util.List" %>
<%@ page import="java.io.File" %>
<%@ page import="com.huan.service.NewsServiceImpl" %>
<%@ page import="com.huan.service.NewsService" %>
<%@ page import="com.huan.entity.News" %>
<%@ page import="java.io.InputStream" %>
<%@ page import="java.io.InputStreamReader" %>
<%@ page import="java.io.BufferedReader" %>
<%@ page import="com.sun.xml.internal.bind.v2.model.core.ID" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
    <head>
        <title>Title</title>
    </head>
    <body>
        <%
            //接收请求
            boolean isMultipart = ServletFileUpload.isMultipartContent(request);
            News news = new News();
            if (isMultipart) {
                DiskFileItemFactory factory = new DiskFileItemFactory();
                ServletFileUpload upload = new ServletFileUpload(factory);
                //将表单每个控件的内容传递
                List<FileItem> items = upload.parseRequest(request);
                if (items != null && items.size() > 0) {
                    for (FileItem item : items) {
                        //将控件内容取出
                        if (item.isFormField()) {//普通的页面组件
                            String fieldName = item.getFieldName();
                            if ("categoryId".equals(fieldName)) {
                                String categoryId = item.getString("UTF-8");
                                news.setCategoryId(categoryId != null && !"".equals(categoryId) ? Integer.valueOf(categoryId) : 0);
                            } else if ("title".equals(fieldName)) {
                                String title = item.getString("UTF-8");
                                news.setTitle(title);
                            } else if ("author".equals(fieldName)) {
                                String author = item.getString("UTF-8");
                                news.setAuthor(author);
                            } else if ("summary".equals(fieldName)) {
                                String summary = item.getString("UTF-8");
                                news.setSummary(summary);
                            } else if ("newscontent".equals(fieldName)) {
                                String content = item.getString("UTF-8");
                                news.setContent(content);
                            } else if ("updateId".equals(fieldName)) {
                                String updateId = item.getString("UTF-8");
                                news.setId(updateId != null && !"".equals(updateId) ? Integer.valueOf(updateId) : 0);
                            }

                        } else {
                            String fileName = item.getName();
                            if (fileName != null && !"".equals(fileName)) {
                                //String uploadFilePath = request.getSession().getServletContext().getRealPath("upload/");
                                String path = "F:\\IJ\\SchoolWeb\\jsp\\src\\main\\webapp\\upload";
                                //为了防止文件名一样，加一个时间戳
                                File file = new File(path + "/" + System.currentTimeMillis() + fileName);
                                try {
                                    item.write(file);

                                }
                                catch (Exception e) {
                                    e.printStackTrace();
                                }
                                String pic = "upload/" + fileName;
                                news.setPicpath(pic);
                            }
                        }
                    }
                }
            } else {
                request.getParameter("");
            }
            //service--save=》dao
            NewsService newsService = new NewsServiceImpl();
            int rows;
            if (news.getId() > 0) { //前端有值传出
                rows = newsService.updateNews(news);
            }else{
                rows = newsService.addNews(news);
            }
            //页面跳转（并进行查询已实现）
            if (rows > 0) {
                request.getRequestDispatcher("newsDetailList.jsp").forward(request, response);
            } else {
                request.getRequestDispatcher("newsDetailCreateSimple.jsp").forward(request, response);
            }
        %>

    </body>
</html>
